package com.landeb.shop.huo.act;

import android.content.Context;
import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.landeb.shop.R;
import com.landeb.shop.huo.MyApplication;
import com.landeb.shop.huo.adapter.ActRechargeLogRVAdapter;
import com.landeb.shop.huo.adapter.ActScoreLogRVAdapter;
import com.landeb.shop.huo.bean.BeanActMoneyLog;
import com.landeb.shop.huo.bean.BeanActRechargeLog;
import com.landeb.shop.huo.httpRequest.ActRechargeLogRequest;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;

import java.util.ArrayList;
import java.util.List;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class RechargeLogActivity extends BaseActivity {

    private LinearLayout btnBack;
    private SwipeRefreshLayout mSRL;
    private RecyclerView mRecyclerView;

    private ActRechargeLogRVAdapter mAdapter;
    private List<BeanActRechargeLog.DataBean.ListBean> mList = new ArrayList<>();

    private Retrofit mRetrofit;
    private ActRechargeLogRequest mRequest;

    private boolean isRefreshing = false;
    private boolean hasNextPage = true;

    private int page = 1;

    public static void toAct(Context context) {

        context.startActivity(new Intent(context, RechargeLogActivity.class));

    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recharge_log);

        OkHttpClient client = MyApplication.getClient();

        mRetrofit = new Retrofit.Builder()
                .baseUrl(MyApplication.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .client(client)
                .build();
        mRequest = mRetrofit.create(ActRechargeLogRequest.class);

        initView();
        initData();
        initEvent();

        refresh();

    }

    private void refresh() {


        if (!isRefreshing) {
            page = 1;
            hasNextPage = true;
//            isLordingMore = false;
            isRefreshing = true;
            mAdapter.loadMoreComplete();
            mSRL.setRefreshing(true);


            mRequest.getData(page + "")
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Subscriber<BeanActRechargeLog>() {
                        @Override
                        public void onCompleted() {
                            mSRL.setRefreshing(false);
                            isRefreshing = false;
                        }

                        @Override
                        public void onError(Throwable e) {
                            mSRL.setRefreshing(false);
                            isRefreshing = false;
                        }

                        @Override
                        public void onNext(BeanActRechargeLog beanActRechargeLog) {

                            if (beanActRechargeLog.getStatus() != 1) {
                                Toast.makeText(RechargeLogActivity.this, beanActRechargeLog.getInfo(), Toast.LENGTH_SHORT).show();
                                return;
                            }

                            mList.clear();
                            mList.addAll(beanActRechargeLog.getData().getList());
                            mAdapter.notifyDataSetChanged();
                        }
                    });
        }

    }

    private void loadMore() {


        if (hasNextPage) {


            page++;


            mRequest.getData(page + "")
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Subscriber<BeanActRechargeLog>() {
                        @Override
                        public void onCompleted() {
                            if (hasNextPage) {
                                mAdapter.loadMoreComplete();
                            } else {
                                mAdapter.loadMoreEnd();
                            }

                        }

                        @Override
                        public void onError(Throwable e) {
                            mAdapter.loadMoreFail();

                        }

                        @Override
                        public void onNext(BeanActRechargeLog beanActRechargeLog) {
                            if (beanActRechargeLog.getStatus() != 1) {
                                Toast.makeText(RechargeLogActivity.this, beanActRechargeLog.getInfo(), Toast.LENGTH_SHORT).show();
                                return;
                            }

                            hasNextPage = (beanActRechargeLog.getData().getMore() == 1);

                            mList.addAll(beanActRechargeLog.getData().getList());
                            mAdapter.notifyDataSetChanged();
                        }
                    });
        }


    }

    private void initEvent() {

        btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

        mSRL.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refresh();
            }
        });

        mAdapter = new ActRechargeLogRVAdapter(R.layout.item_act_recharge_log_tv, mList);
        mAdapter.setEnableLoadMore(true);
        mAdapter.setAutoLoadMoreSize(5);
        mAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                loadMore();
            }


        });

        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setAdapter(mAdapter);
        mRecyclerView.addItemDecoration(
                new HorizontalDividerItemDecoration.Builder(this)
                        .color(getResources().getColor(R.color.background))
                        .size(1)
                        .build());

    }


    private void initData() {

    }

    private void initView() {
        btnBack = (LinearLayout) findViewById(R.id.btn_back_recharge_log_act);
        mSRL = (SwipeRefreshLayout) findViewById(R.id.srl_recharge_log_act);
        mSRL.setColorSchemeColors(getResources().getColor(R.color.colorPrimary));
        mRecyclerView = (RecyclerView) findViewById(R.id.rv_recharge_log_act);
    }
}
